package com.azt.provider.impl;

import com.azt.api.pojo.ProRepository;
import com.azt.api.service.ProRepositoryService;
import com.azt.dao.JedisClient;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.ProRepositoryMapper;
import com.azt.utils.MyStringUtils;
import com.azt.utils.PasswordUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service
@Transactional
public class ProRepositoryServiceImpl implements ProRepositoryService {
	
	private static Logger logger = LoggerFactory.getLogger(ProRepositoryServiceImpl.class);

	@Autowired
	private ProRepositoryMapper mapper;
	
	@Autowired
	private JedisClient  jedis;

	public  static final String PCATS="pcats";

	@Override
	public Pagination getRepositoryByPage(int pageno, int pagesize, Map<String, String> searchMap) {
		
		Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		List<ProRepository>  list =   mapper.getProRepositoryList(searchMap);
		Pagination p = new Pagination(startPage, list);
		p.setParams(searchMap);
		return p;
	}

	@Override
	public ProRepository getEntityById(Integer id) {
		return mapper.selectOne(new ProRepository(id));
	}

	@Override
	public void saveOrUpdateEntity(ProRepository pr) {
		if(pr.getId()==null){
			mapper.insert(pr);
		}else{
			mapper.updateByPrimaryKey(pr);
		}
	}

	@Override
	public void deleteEntity(ProRepository pr) {
		pr = mapper.selectByPrimaryKey(pr);
		//jedis.hdel(PCATS,pr.getSecret());
		mapper.deleteByPrimaryKey(pr);
	}
	
	

	@Override
	public boolean ifTitleExist(String md5 , Integer categoryId) {
		ProRepository pr = new ProRepository();
		pr.setSecret(md5);
		pr.setPid(categoryId);
		List<ProRepository> listByParams = mapper.select(pr);
		if(listByParams==null || listByParams.size() == 0){
			return true;
		}
		return false;
	}

	@Override
	public void deleteByCategoryId(Integer categoryId) {
		ProRepository pr = new ProRepository();
		pr.setPid(categoryId);
		mapper.delete(pr);
	}
	
	//获取类别
	private String jedisGet(String md5_16){
		return jedis.hget(PCATS, md5_16);
	}
	
	//设置类别
	private void jedisSet(String md5_16,String pcatids){
		jedis.hset(PCATS, md5_16, pcatids);
	}
	//jedis 删除
	private void jedisDel(String md5_16){
		jedis.hdel(PCATS, md5_16);
	}
	
	
	
	@Override
	public String getPcatidBymd5(String md5_16) {
		String pids = null;
		String hget = jedisGet(md5_16);
		if(hget!=null){
			return hget;
		}

		List<ProRepository> repositorys = getRepositoryBymd5(md5_16);
		if(repositorys==null || repositorys.isEmpty()){
			//不存在返回null
		}else{
			StringBuffer sBuffer  = new StringBuffer();
			boolean isfirst = true;
			for (int i = 0; i < repositorys.size(); i++) {
				if(isfirst){
					isfirst = false;
				}else{
					sBuffer.append(",");
				}
				ProRepository proRepository = repositorys.get(i);
				String categoryid = proRepository.getProCategory().getId()+"";
				sBuffer.append(categoryid);
			}
			pids = sBuffer.toString();
		}
		

//		jedis set
		if(pids!=null){
			jedisSet(md5_16, pids);
		}
		return pids;
	}


	@Override
	public String getPcatidByTitle(String title) {
		String trim = MyStringUtils.replaceBlank(title);
		String md5_16 = PasswordUtil.MD5_16(trim);
		return getPcatidBymd5(md5_16);
	}

	@Override
	public ProRepository saveOrupdateRepository(ProRepository repository) {
		if(repository != null) {
			if (repository.getId() == null) {
				mapper.insert(repository);
			} else {
				mapper.updateByPrimaryKey(repository);
			}
			//更新缓存,如果缓存中已经有了,就删除掉,下次查询会再set进去,没有的话就保存一条缓存
			String secret = repository.getSecret();
			String hget = jedisGet(secret);

			if (hget != null) {
				jedisDel(secret);
			} else {
				jedisSet(secret, repository.getPid() + "");
			}
		}
		return repository;
	}

	@Override
	public List<ProRepository> getRepositoryBymd5(String md5_16) {
		ProRepository proRepository = new ProRepository();
		proRepository.setSecret(md5_16);
		List<ProRepository>  list =  mapper.getRepositoryAndCategoryBymd5(md5_16);
		return list;
	}
	
	

	@Override
	public void deleteMore(String[] ids) {
		mapper.deleteByIds(ids);
	}

	@Override
	public void deleteByCategoryIdAndSecret(String secret, Integer categoryid) {
		
		ProRepository proRepository = new ProRepository();
		proRepository.setSecret(secret);
		proRepository.setPid(categoryid);
		mapper.delete(proRepository);
		
		deleredis(secret);
	}
	
	
	private void deleredis(String secret){
		String hget = jedisGet(secret);
		if(hget!=null){
			jedisDel(secret);
		}
	}

	@Override
	public ProRepository getRepositoryBymd5AndPcat(String md5secret, int pid) {
		ProRepository proRepository = new ProRepository();
		proRepository.setSecret(md5secret);
		proRepository.setPid(pid);
		return mapper.selectOne(proRepository);
	}

	public List<ProRepository> getRepositoryByPidAndSecret(String md5secret, int pid) {
		ProRepository proRepository = new ProRepository();
		proRepository.setSecret(md5secret);
		proRepository.setPid(pid);
		return mapper.select(proRepository);
	}
}
